﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using MySql.Data.MySqlClient;
using System.Data.SqlClient;
namespace DMU.Gui.GuiPro
{
    public partial class frmThemGiangVien : DevExpress.XtraEditors.XtraForm
    {


        Service sv = new Service();
        help hp = new help();
        private ucGiangVien ucGV;

        public frmThemGiangVien(ucGiangVien uc)
        {
            InitializeComponent();
            ucGV = uc;
        }

        private void frmThemGiangVien_Load(object sender, EventArgs e)
        {
            
            lkpMaBM.Properties.DataSource = ucGV.dtBM;
            lkpMaBM.Properties.DisplayMember = "TenBM";
            lkpMaBM.Properties.ValueMember = "MaBM";


        }

        private SqlParameter[] getPara(string MS, string ho, string ten, string ngS, string maBM, string gt, string matKhau,string ghiChu, bool active)
        {
            SqlParameter[] pr = new SqlParameter[10];
            pr[0] = new SqlParameter("MaGV0", MS);
            pr[1] = new SqlParameter("HoLot0", ho);
            pr[2] = new SqlParameter("Ten0", ten);
            pr[3] = new SqlParameter("NgaySinh0",ngS);
            pr[4] = new SqlParameter("MaBM0", maBM);
            pr[5] = new SqlParameter("GioiTinh0", gt);
            pr[6] = new SqlParameter("MatKhau0", matKhau);
            pr[7] = new SqlParameter("IsActive0", active);
            pr[8] = new SqlParameter("GhiChu0", ghiChu);
            pr[9] = new SqlParameter("MaTruong0", Service.info[4]);
            return pr;
        }

        private SqlParameter[] getPara(string MS, string ho, string ten, string ngS, string maBM, bool gt, string matKhau, string ghiChu, bool active)
        {
            SqlParameter[] pr = new SqlParameter[10];
            pr[0] = new SqlParameter("MaGV0", MS);
            pr[1] = new SqlParameter("HoLot0", ho);
            pr[2] = new SqlParameter("Ten0", ten);
            pr[3] = new SqlParameter("NgaySinh0", ngS);
            pr[4] = new SqlParameter("MaBM0", maBM);
            pr[5] = new SqlParameter("GioiTinh0", gt);
            pr[6] = new SqlParameter("MatKhau0", matKhau);
            pr[7] = new SqlParameter("IsActive0", active);
            pr[8] = new SqlParameter("GhiChu0", ghiChu);
            pr[9] = new SqlParameter("MaTruong0", Service.info[4]);
            return pr;
        }

        private string[] getInfo()
        {
            string[] st = new string[9];
            st[0] = txtMa.Text;
            st[1] = txtHo.Text;
            st[2] = txtTen.Text;
            st[3] = txtMK.Text;
            st[4] = chkNam.Checked.ToString();
            st[5] = dteNgaySinh.Text;
            st[6] = lkpMaBM.EditValue.ToString();
            st[7] = chkActive.Checked.ToString();
            st[8] = txtghiChu.Text;
            return st;

        }

        private void btnLuu_Click(object sender, EventArgs e)
        {
            
            if (!dxValidationProvider1.Validate())
            {
                return;
            }
            try
            {
                SqlParameter[] pr = getPara(txtMa.Text, txtHo.Text, txtTen.Text,dteNgaySinh.Text, lkpMaBM.EditValue.ToString(),chkNam.Checked, txtMK.Text,txtghiChu.Text, chkActive.Checked);
                if (ucGV.them)
                {
                    string s = sv.executeScalarStore("dmu_proGVThem", pr);
                   if (s == "0")
                    {
                        hp.showMes("Mã này đã tồn tại vui lòng chọn mã khác !", -1);
                        txtMa.Focus();
                    }
                    else
                    {
                        
                        sv.addRow(ucGV.dtGV, getInfo());
                        txtMa.Text = txtTen.Text = txtHo.Text =txtMK.Text= ""; txtMa.Focus();
                    }

                }
                else
                {
                    if (sv.executeNonqueryStore("dmu_proGVSuu", pr))
                    {
                        DataRow row = ucGV.dtGV.Rows[ucGV.BindingContext[ucGV.dtGV].Position];
                        sv.updateRow(row, getInfo());
                        this.Close();
                    }
                    else
                        hp.showMes("Thao tác không thành công !. Kiểm tra lại dữ liệu ", 1);
                }
            }
            catch (Exception et)
            {
                hp.showMes("Thao tác không thành công !! Lỗi" + et.Message, 1);
            }
        }

        private void cbbPath_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            string path = hp.openFile();
            if (path != "")
            {
                try
                {
                    cbbPath.Text = path;
                    cbbSheetName.Properties.Items.Clear();
                    cbbSheetName.Properties.Items.AddRange(sv.getEcelSheetName(path));
                    cbbSheetName.Focus();
                }
                catch (Exception et) { hp.showMes("Lỗi khi load sheetName. Vui lòng thử lại ! " + et.Message, 1); }
            }
        }

        private void btnHoanTat_Click(object sender, EventArgs e)
        {
            if (!dxValidationProvider2.Validate())
            {
                if (cbbPath.Text == "")
                {
                    cbbPath.Focus(); return;
                }
                else
                {
                    cbbSheetName.Focus(); return;
                }
            }
            
            DataTable dt = sv.excelToDatatable(cbbPath.Text,"select MaGV,HoLot,Ten,MatKhau,GioiTinh,NgaySinh,MaBM,GhiChu from [" + cbbSheetName.Text + "]");
            DataColumn cl = new DataColumn("IsActive",Type.GetType("System.Boolean"));
            dt.Columns.Add(cl);

            if (dt.Rows.Count > 0)
            {
                int dem = 0; int countRow = dt.Rows.Count;
                //try
                {
                    sv.openConnect();
                    SqlParameter[] pr;

                    foreach (DataRow row in dt.Rows)
                    {
                        if (row[0]!= null && row[0].ToString() != "" && row["MaBM"] !=null || row["MaBM"].ToString().Trim() != "")
                        {
                            
                            try
                            {
                                pr = getPara(row[0].ToString(), row[1].ToString(), row[2].ToString(),row["NgaySinh"].ToString(), row["MaBM"].ToString(),row["GioiTinh"].ToString(), row["MatKhau"].ToString(), row["GhiChu"].ToString(), true);

                            }
                            catch (Exception)
                            {
                                continue;
                            }

                            if (sv.executeNonqueryStore("dmu_proGVThem1", pr))
                            {
                                dem++;
                                if (row["GioiTinh"].ToString() == "" || row["GioiTinh"] == null)
                                {
                                    row["GioiTinh"] = true;
                                    
                                }
                                else
                                    row["GioiTinh"] = false;

                                dt.AcceptChanges();

                                row["IsActive"] = true;
                                try
                                {
                                    ucGV.dtGV.ImportRow(row);
                                }
                                catch (Exception) { }
                            }
                        }
                    }

                    dt.Dispose();
                    sv.closeConnect();
                    hp.showMes("Thêm thành công " + dem + "/" + countRow, -1);
                }

            }
        }

        private void chkNam_CheckedChanged(object sender, EventArgs e)
        {

        }

        private void btnDong_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void txtMa_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == 13)
            {
                EventArgs et = new EventArgs();
                btnLuu_Click(sender, et);
            }
        }


    }
}